import {observable, action} from "mobx";
import request from "../utils/request";
import * as api from "../api/api";
import {LoadHoc, loading} from '../hoc/loadHoc';

export default class PointMallStore extends LoadHoc {
  /**
   * Banner
   */
  @observable home: api.YNJWTPointMallVOHomeVO = {};

  @loading @action
  async getHomeInfo(params: {
    callback?: (home: api.YNJWTPointMallVOHomeVO) => void;
  }) {
    const {callback} = params;
    const home = await request(api.ApppointMallApiFetchParamCreactor.apppointMallGetHomeAsync({}));
    this.home = home;
    callback && callback(home);
  }

  /**
   * 首页商品信息
   */
  @observable allItemsInfo: api.YNJWTPointMallVOHomeVO = {};

  @loading @action
  async getAllItemsInfo(params: {
    formRecommendCategoryId?: number,
    pageIndex: number,
    pageSize: number,
    callback?: (res: api.YNJWTPointMallVOHomeVO) => void;
  }) {
    const {formRecommendCategoryId,pageIndex,pageSize,callback} = params;
    const allItemsInfo = await request(api.ApppointMallApiFetchParamCreactor.apppointMallGetAllItemsInfoAsync({
      formRecommendCategoryId,
      formSkipCount:  (pageIndex-1)*pageSize,
      formMaxResultCount:pageSize
    }));
    this.allItemsInfo = allItemsInfo;
    callback && callback(allItemsInfo);
  }

  /**
   * 商品详情
   */
  @observable itemInfo: api.YNJWTPointMallVOItemAppVO = {};

  @loading @action
  async getItemInfo(params: {
    formId:number,
    callback?: (itemInfo: api.YNJWTPointMallVOItemAppVO) => void;
  }) {
    const {formId,callback} = params;
    const itemInfo = await request(api.ApppointMallApiFetchParamCreactor.apppointMallGetItemAsync({
      formId
    }));
    this.itemInfo = itemInfo;
    callback && callback(itemInfo);
  }

  /**
   * 获取用户默认地址
   */
  @observable userDefaultAddress: api.YNJWTUsersVOUserAddressVO = {};

  @loading @action
  async getUserDefaultAddress(params: {
    callback?: (itemInfo: api.YNJWTUsersVOUserAddressVO) => void;
  }) {
    const {callback} = params;
    const userDefaultAddress = await request(api.ApppointMallApiFetchParamCreactor.apppointMallGetUserDefaultAddress({
    }));
    this.userDefaultAddress = userDefaultAddress;
    callback && callback(userDefaultAddress);
  }

  /**
   * 购买(下单)
   */
  @observable orderDetail: api.YNJWTPointMallVOOrderVO = {};

  @loading @action
  async placeOrder(params: {
    form:{
      itemSpecId: number,
      qty: number,
      message: string,
      addressId: number,
      orderType: number,
    },
    callback?: (res: api.YNJWTPointMallVOOrderVO) => void;
  }) {
    const {form,callback} = params;
    const orderDetail = await request(api.ApppointMallApiFetchParamCreactor.apppointMallBuyAsync({
      form
    }));
    this.orderDetail = orderDetail;
    callback && callback(orderDetail);
  }

  /**
   * 订单列表
   */
  @observable orders: api.YNJWTPointMallVOOrderMiniVO = {};

  @loading @action
  async getOrders(params: {
    formStatus:number,
    pageIndex:number,
    pageSize:number,
    callback?: (res: api.YNJWTPointMallVOOrderMiniVO) => void;
  }) {
    const {formStatus,pageIndex,pageSize,callback} = params;
    const orders = await request(api.ApppointMallApiFetchParamCreactor.apppointMallGetOrdersAsync({
      formStatus,
      formSkipCount: (pageIndex-1)*pageSize,
      formMaxResultCount: pageSize
    }));
    this.orders = orders;
    callback && callback(orders);
  }

  /**
   * 订单详情
   */
  @observable order: api.YNJWTPointMallVOOrderVO = {};

  @loading @action
  async getOrder(params: {
    formId:number,
    callback?: (home: api.YNJWTPointMallVOOrderVO) => void;
  }) {
    const {formId,callback} = params;
    const order = await request(api.ApppointMallApiFetchParamCreactor.apppointMallGetOrderAsync({
      formId
    }));
    this.itemInfo = order;
    callback && callback(order);
  }

  /**
   * 修改未支付订单地址
   */

  @loading @action
  async updateUserReceiver(params: {
    form: {
      orderId: number,
      receiverId: number
    },
    callback?: () => void;
  }) {
    const {form,callback} = params;
    await request(api.ApppointMallApiFetchParamCreactor.apppointMallUpdateUserReceiverAsync({
      form
    }));
    callback && callback();
  }

  /**
   * 支付
   */
  @loading @action
  async pay(params: {
    form:{
      orderId: number,
      payType: api.YNJWTPointMallFormOrderPayFormPayTypeEnum,
      isPayPoints: boolean
    },
    callback?: (res: api.YNJWTPointMallVOOrderVO) => void;
  }) {
    const {form,callback} = params;
    const res = await request(api.ApppointMallApiFetchParamCreactor.apppointMallPayAsync({
      form
    }));
    callback && callback(res);
  }

  /**
   * 使用积分支付
   */
  @loading @action
  async buyItemWithPoints(params: {
    form:{
      orderId: number,
    },
    callback?: (res: api.YNJWTPointMallVOOrderVO) => void;
  }) {
    const {form,callback} = params;
    const res = await request(api.ApppointMallApiFetchParamCreactor.apppointMallBuyItemWithPointsAsync({
      form
    }));
    callback && callback(res);
  }

  /**
   * 取消订单
   */
  @loading @action
  async cancelOrder(params: {
    form:api.YNJWTCommonSingleIdSystemInt64,
    callback?: (res) => void;
  }) {
    const {form,callback} = params;
    const res = await request(api.ApppointMallApiFetchParamCreactor.apppointMallCancelAsync({
      form
    }));
    callback && callback(res);
  }

  /**
   * 根据现有订单再下一单
   */
  @loading @action
  async placeNewOrder(params: {
    id: number ,
    callback?: (res: api.YNJWTPointMallVOOrderVO) => void;
  }) {
    const {id,callback} = params;
    const res = await request(api.ApppointMallApiFetchParamCreactor.apppointMallNewOrderAsync({
      form: { id }
    }));
    callback && callback(res);
  }

  /**
   * 确认收货
   */
  @loading @action
  async confirmGet(params: {
    form:api.YNJWTCommonSingleIdSystemInt64,
    callback?: (res: api.YNJWTPointMallVOOrderVO) => void;
  }) {
    const {form,callback} = params;
    const res = await request(api.ApppointMallApiFetchParamCreactor.apppointMallConfirmGetAsync({
      form
    }));
    callback && callback(res);
  }

  /**
   * 删除订单
   */
  @loading @action
  async deleteOrder(params: {
    form:api.YNJWTCommonSingleIdSystemInt64,
    callback?: (res: api.YNJWTPointMallVOOrderVO) => void;
  }) {
    const {form,callback} = params;
    const res = await request(api.ApppointMallApiFetchParamCreactor.apppointMallDeleteOrderAsync({
      form
    }));
    callback && callback(res);
  }


  /**
   * 物流信息
   */
  @observable orderLogistics: api.YNJWTPointMallVOOrderVO = {};

  @loading @action
  async getOrderLogistics(params: {
    formId:number,
    callback?: (home: api.YNJWTPointMallVOOrderVO) => void;
  }) {
    const {formId,callback} = params;
    const orderLogistics = await request(api.ApppointMallApiFetchParamCreactor.apppointMallGetOrderLogisticsAsync({
      formId
    }));
    this.orderLogistics = orderLogistics;
    callback && callback(orderLogistics);
  }

  /**
   * 礼品定制分类
   */
  @loading @action
  async getAllCustomizedCategory(params: {
    callback?: (res: api.YNJWTPointMallVOCustomizedCategoryVO) => void;
  }) {
    const {callback} = params;
    const allCustomizedCategory = await request(api.ApppointMallApiFetchParamCreactor.apppointMallGetAllCustomizedCategoryAsync({
    }));
    callback && callback(allCustomizedCategory);
  }

  /**
   * 礼品定制列表
   */
  @observable customizedInfo: api.YNJWTPointMallVOOrderMiniVO = {};
  @loading @action
  async getCustomizedInfo(params: {
    formCustomizedCategoryId:number,
    pageIndex:number,
    pageSize:number,
    callback?: (res: api.YNJWTPointMallVOOrderMiniVO) => void;
  }) {
    const {formCustomizedCategoryId,pageIndex,pageSize,callback} = params;
    const customizedInfo = await request(api.ApppointMallApiFetchParamCreactor.apppointMallGetCustomizedInfoAsync({
      formCustomizedCategoryId,
      formSkipCount: (pageIndex-1)*pageSize,
      formMaxResultCount: pageSize
    }));
    this.customizedInfo = customizedInfo;
    callback && callback(customizedInfo);
  }


  /**
   * 礼品定制详情
   */
  @observable customized: api.YNJWTPointMallVOcustomizedVO = {};

  @loading @action
  async getCustomized(params: {
    formId:number,
    callback?: (res: api.YNJWTPointMallVOcustomizedVO) => void;
  }) {
    const {formId,callback} = params;
    const customized = await request(api.ApppointMallApiFetchParamCreactor.apppointMallGetCustomizedAsync({
      formId
    }));
    this.customized = customized;
    callback && callback(customized);
  }
}
